<script setup>
import { ref } from 'vue'
import * as userApi from "@/api/userApi"
import { ElMessageBox, ElMessage } from 'element-plus';
import { useUserInfoStore } from '@/stores/user';
import { useTokenStore } from '@/stores/token';
import { useRouter } from 'vue-router';
const router = useRouter();
const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
const userInfo = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: '',
})
const rules = {
    old_pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '昵称必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    new_pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '昵称必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    re_pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '昵称必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
}

const submitResetPwd = async () => {
    ElMessageBox.confirm('确认提交吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        // 提交修改
        let result = await userApi.userResetPassword(userInfo.value)
        if (result.code === 0) {
            userInfoStore.removeInfo()
            tokenStore.removeToken()
            ElMessage.success("修改成功")
            setTimeout(() => {
                router.push('/login')
            }, 1000)
        } else{}
    })
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="150px" size="large">
                    <el-form-item label="旧密码" prop="old_pwd">
                        <el-input v-model="userInfo.old_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input v-model="userInfo.new_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="再次确认新密码" prop="re_pwd">
                        <el-input v-model="userInfo.re_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitResetPwd">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>